/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dai.grupovictor.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dai.grupovictor.dao.CursoDao;
import pe.edu.upc.dai.grupovictor.dao.util.ConnectionUtils;
import pe.edu.upc.dai.grupovictor.dao.util.DaoException;
import pe.edu.upc.dai.grupovictor.model.Curso;

public class CursoDaoImpl implements CursoDao {

    public Curso seleccionarPorPrimaryKey(Long idCurso) {
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        Curso curso = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.prepareStatement("select * from CURSO where ID_CURSO=?");
            st.setLong(1, idCurso);
            rs = st.executeQuery();
            if (rs.next()) {
                curso = new Curso();
                curso.setCodigoCurso(rs.getString("CODIGO_CURSO"));
                curso.setIdCurso(rs.getLong("ID_CURSO"));
                curso.setNombreCurso(rs.getString("NOMBRE_CURSO"));

            }
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return curso;
    }

    public Curso seleccionarPorNombre(String nombre) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void insertar(Curso Curso) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actualizar(Curso Curso) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void eliminar(Curso Curso) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Curso> seleccionarTodos() {
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        Curso Curso = null;
        List<Curso> lista = new ArrayList<Curso>();
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.prepareStatement("Select * from Curso");
            rs = st.executeQuery();
            while (rs.next()) {
                Curso = mapResultSet(rs);
                lista.add(Curso);
            }
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return lista;
    }

    private Curso mapResultSet(ResultSet rs) throws SQLException {
        Curso curso = new Curso();
        curso.setCodigoCurso(rs.getString("CODIGO_CURSO"));
        curso.setIdCurso(rs.getLong("ID_CURSO"));
        curso.setNombreCurso(rs.getString("NOMBRE_CURSO"));

        return curso;
    }
}
